package com.yyp.car.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hdw.basic.dao.BaseDao;
import com.yyp.car.Idao.IUserDao;
import com.yyp.car.model.User;

@Repository("userDao")
public class UserDao extends BaseDao<User> implements IUserDao {

	@Override
	public User findByUserName(String username) {
		String hql = "from User u where u.username=?";
		return (User) this.queryObject(hql, username);
	}

	@Override
	public List<User> listTeacher() {
		String hql="from User u where u.teacherMes is not null";
		return this.list(hql);
	}

	@Override
	public List<User> listStudent() {
		String hql="from User u where u.studentMes is not null and u.testType is not null and u.testType!='' order by ttid desc";
		return this.list(hql);
	}

	@Override
	public User findById(int id) {
		String hql="from User u where u.id='"+id+"'";
		return (User)this.queryObject(hql);
	}

	@Override
	public List<User> listStudentByTid(int tid) {
		String hql="from User u where u.studentMes is not null and u.testType is not null and u.testType!='' and u.teacher.id='"+tid+"' order by ttid desc";
		return this.list(hql);
	}

	@Override
	public String findPassword(int id) {
		String hql="select u.password from User u where u.id='"+id+"'";
		return (String)this.queryObject(hql);
	}

	@Override
	public void changePassword(int id, String password) {
		String hql="update User u set u.password='"+password+"' where u.id='"+id+"'";
		this.getSession().createQuery(hql).executeUpdate();
	}

	@Override
	public List<User> listAllStudent() {
		String hql="from User u where u.studentMes is not null order by status asc";
		return this.list(hql);
	}

	@Override
	public void blackStudent(int id) {
		String hql="update User u set u.status=1 where u.id=?";
		this.updateByHql(hql, id);
	}

	@Override
	public void whiteStudent(int id) {
		String hql="update User u set u.status=0 where u.id=?";
		this.updateByHql(hql, id);
	}

}
